package com.zhouyibike.uu.ui.activity.xingcheng;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ZoomControls;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.route.BikingRouteResult;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.IndoorRouteResult;
import com.baidu.mapapi.search.route.MassTransitRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRouteLine;
import com.baidu.mapapi.search.route.WalkingRoutePlanOption;
import com.baidu.mapapi.search.route.WalkingRouteResult;
import com.zhouyibike.uu.R;
import com.zhouyibike.uu.entity.MyJourneyResult;
import com.zhouyibike.uu.ui.base.BaseActivity;
import com.zhouyibike.uu.utils.TimeUtils;
import com.zhouyibike.uu.utils.WalkingRouteOverlay;

public class JourneyDetailActivity extends BaseActivity implements View.OnClickListener,OnGetRoutePlanResultListener {
    private ImageButton mBtnBack;
    private TextView mBtnTitle;
    private MyJourneyResult.DataBean detail;
    private MapView mapView;
    private BaiduMap mBaiduMap;
    private WalkingRouteOverlay overlay;
    private RoutePlanSearch mSearch = null;    // 搜索模块，也可去掉地图模块独立使用
    private TextView tv_plantnum;
    private LinearLayout ll_isshow;
    private ImageView iv_isshow;
    private boolean isshow=false;
    private TextView tv_totalmoney,tv_ycq,tv_pay,tv_time,tv_dj;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_journey_detail);
        detail= (MyJourneyResult.DataBean) getIntent().getSerializableExtra("detail");
        mSearch = RoutePlanSearch.newInstance();
        mSearch.setOnGetRoutePlanResultListener(this);
        initView();

        MarkerOptions markerOptions;
            LatLng latLng=new LatLng(detail.getStartLatitude(),detail.getStartLongitude());
            BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.mipmap.shi);
            markerOptions = new MarkerOptions().icon(bitmap).position(latLng);
            mBaiduMap.addOverlay(markerOptions);

        LatLng latLngend=new LatLng(detail.getEndLatitude(),detail.getEndLongitude());
        BitmapDescriptor bitmapend = BitmapDescriptorFactory.fromResource(R.mipmap.zhong);
        markerOptions = new MarkerOptions().icon(bitmapend).position(latLngend);
        mBaiduMap.addOverlay(markerOptions);


        PlanNode stNode = PlanNode.withLocation(new LatLng(detail.getStartLatitude(),detail.getStartLongitude()));
        PlanNode enNode = PlanNode.withLocation(new LatLng(detail.getEndLatitude(),detail.getEndLongitude()));
        mSearch.walkingSearch(new WalkingRoutePlanOption().from(stNode).to(enNode));
    }
    LocationClient mLocClient;

    private void locationmsg(){
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        mBaiduMap.setMyLocationEnabled(true);
        //点击获取路径规划      lylmarketclick

        mLocClient = new LocationClient(this);
        mLocClient.registerLocationListener(myListener);
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(3000);
        mLocClient.setLocOption(option);
        mLocClient.start();
    }
    private void initView() {
        mBtnBack = (ImageButton) findViewById(R.id.bar_btn_back);
        mBtnTitle = (TextView) findViewById(R.id.bar_text_title);
        mBtnTitle.setText("行程详情");
        mBtnBack.setOnClickListener(this);

        mapView= (MapView) findViewById(R.id.detail_map);
        mapView.showZoomControls(false);//缩放去掉
        mBaiduMap = mapView.getMap();
        mapView.showScaleControl(false);//去除比例尺控件
        View child = mapView.getChildAt(1);//logo去掉
        if (child != null && (child instanceof ImageView || child instanceof ZoomControls)) {
            child.setVisibility(View.INVISIBLE);
        }
        tv_plantnum= (TextView) findViewById(R.id.tv_detail_plantnum);
        tv_plantnum.setText(detail.getPlateNumber());
        ll_isshow= (LinearLayout) findViewById(R.id.ll_detail_isshowmsg);
        iv_isshow= (ImageView) findViewById(R.id.imageView_isshow);
        iv_isshow.setOnClickListener(this);
        tv_totalmoney= (TextView) findViewById(R.id.tv_detail_money);
        tv_totalmoney.setText(detail.getTotalAmount()+"元");
        tv_ycq= (TextView) findViewById(R.id.tv_detail_ycq);
        tv_ycq.setText(detail.getPackageAmount()+"元");
        tv_pay= (TextView) findViewById(R.id.tv_detail_pay);
        tv_pay.setText(detail.getWalletAmount()+"元");
        tv_time= (TextView) findViewById(R.id.tv_detail_time);
        double  timehapast=0;
        try {
            timehapast = (TimeUtils.stringToLong(detail.getEndTime(), "yyyy-MM-dd HH:mm")-TimeUtils.stringToLong(detail.getStartTime(), "yyyy-MM-dd HH:mm"))/1000;
        }catch (Exception e){

        }
        tv_time.setText(timehapast/60+"分");
        tv_dj= (TextView) findViewById(R.id.tv_detail_pay_dj);
        tv_dj.setText(preferences.getString("money_unit","1")+"元/小时");
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bar_btn_back:
                finish();
                break;
            case R.id.imageView_isshow:
                if(isshow){
                    isshow=false;
                    iv_isshow.setImageResource(R.mipmap.zhankai);
                    ll_isshow.setVisibility(View.GONE);
                }else{
                    isshow=true;
                    iv_isshow.setImageResource(R.mipmap.shouqi);
                    ll_isshow.setVisibility(View.VISIBLE);
                }
                break;
        }
    }
    /**
     * 我的位置
     */
    boolean isFirstLoc = true; // 是否首次定位
    public MyLocationListenner myListener = new MyLocationListenner();
    public class MyLocationListenner implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            if (location == null || mapView == null) {
                return;
            }


            MyLocationData locData = new MyLocationData.Builder().accuracy(location.getRadius())
                    // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(100).latitude(location.getLatitude()).longitude(location.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);

            if (isFirstLoc) {
                isFirstLoc = false;
                /**
                 * 地图定位到当前位置
                 */
                LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
                MapStatus.Builder builder = new MapStatus.Builder();
                builder.target(ll).zoom(17.0f);
                mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));

            }
        }

        public void onReceivePoi(BDLocation poiLocation) {
        }

        public void onConnectHotSpotMessage(String s, int i){

        }
    }
    //路径规划相关=======================================================================================start
    private class MyDrivingRouteOverlay extends WalkingRouteOverlay {
        public MyDrivingRouteOverlay(BaiduMap baiduMap) {
            super(baiduMap);
        }
        @Override
        public int getLineColor() {
            return getResources().getColor(R.color.color_ljgh);
        }
        @Override
        public BitmapDescriptor getStartMarker() {
            //自定义的起点图标
            return BitmapDescriptorFactory.fromResource(R.mipmap.point);
        }
        @Override
        public BitmapDescriptor getTerminalMarker() {
            //自定义的终点图标
            return BitmapDescriptorFactory.fromResource(R.mipmap.point);
        }
        @Override
        public BitmapDescriptor getNoteMarker() {
            //自定义的终点图标
            return BitmapDescriptorFactory.fromResource(R.mipmap.note_point);
        }

    }
    @Override
    public void onGetWalkingRouteResult(WalkingRouteResult walkingRouteResult) {
        // 获取步行线路规划结果
        if (walkingRouteResult == null || walkingRouteResult.error != SearchResult.ERRORNO.NO_ERROR) {
            toastShow("抱歉，路径规划错误");
        }
        if (walkingRouteResult.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
            toastShow( "抱歉，路径规划错误");
            return;
        }
        if (walkingRouteResult.error == SearchResult.ERRORNO.NO_ERROR) {
            WalkingRouteLine walkingRouteLine=walkingRouteResult.getRouteLines().get(0);
//            distence=walkingRouteLine.getDistance()+"";
//            usetime=walkingRouteLine.getDuration()/60+"";
            overlay = new MyDrivingRouteOverlay(mBaiduMap);
            mBaiduMap.setOnMarkerClickListener(overlay);
            overlay.setData(walkingRouteResult.getRouteLines().get(0));
            overlay.addToMap();
            overlay.zoomToSpan();

        }
    }

    @Override
    public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) {

    }

    @Override
    public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) {

    }

    @Override
    public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) {

    }

    @Override
    public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) {

    }

    @Override
    public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {

    }
    @Override
    protected void onPause() {
        mapView.onPause();
        super.onPause();
    }
    @Override
    protected void onResume() {
        mapView.onResume();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        // 退出时销毁定位
//        mLocClient.stop();
        // 关闭定位图层
        mBaiduMap.setMyLocationEnabled(false);
        mapView.onDestroy();
        mapView = null;
        super.onDestroy();
    }
}
